package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.bo.pay.PayOrderLogBO;
import com.ztgf.order.models.bo.pay.PayOrderSaveBO;
import com.ztgf.order.models.service.pay.IPayOrderLogService;
import com.ztgf.order.models.service.pay.IPayService;
import com.ztgf.order.models.vo.pay.PayOrderLogVO;
import com.ztgf.order.models.vo.pay.PayOrderVO;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneyDeductionBO;
import com.ztgf.shopping.models.dto.shop.ShopEarnestMoneyDTO;
import com.ztgf.shopping.models.service.shop.IShopEarnestMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 店铺保证金控制器
 *
 * @author zhanghualang
 * @Date 2020-06-16 14:38:24
 */

@RestController
@RequestMapping("/cms/shopEarnestMoney")
@Slf4j
@Api(value = "ShopEarnestMoneyController",tags = "店铺保证金接口")
public class ShopEarnestMoneyController {

    @DubboReference
    private IShopEarnestMoneyService shopEarnestMoneyService;

    @DubboReference
    private IPayService payService;

    @ApiOperation(value = "根据店铺ID获取保证金信息",notes = "根据店铺ID获取保证金信息")
    @PostMapping(value = "/getByShopId")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopEarnestMoneyDTO> getByShopId(@RequestParam("id") Long id) {
        ShopEarnestMoneyDTO dto = shopEarnestMoneyService.getByShopId(id);
        if (dto != null){
            dto.setPayable (dto.getBalance ().add (dto.getVacancyAmount ()));
            return new Result<>(dto);
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改应缴纳保证金",notes = "修改应缴纳保证金")
    @PostMapping(value = "/updatePayable")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "payable",value = "应缴纳保证金",required = true,paramType = "query", dataType = "BigDecimal")
    })

    public Result updatePayable(@RequestParam("id") Long id, BigDecimal payable) {
        ShopEarnestMoneyDTO dto = shopEarnestMoneyService.selectById(id);
//        if (dto.getBalance().compareTo(payable) >= 0){
//            return new Result<>(CodeEnum.FAIL_100069);
//        }

        boolean falg = shopEarnestMoneyService.updatePayable(id, payable);
        if (falg){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

//    @ApiOperation(value = "扣除保证金",notes = "扣除保证金")
//    @PostMapping(value = "/deductionEarnest")
//    public Result deductionEarnest(ShopEarnestMoneyDeductionBO bo) {
//        ShopEarnestMoneyDTO dto = shopEarnestMoneyService.selectById(bo.getId());
//        if (bo.getMoney().compareTo(dto.getBalance()) >= 0){
//            return new Result<>(CodeEnum.FAIL_100044);
//        }
//
//        return shopEarnestMoneyService.updateDeductionEarnest(bo);
//    }

    @ApiOperation(value = "缴纳保证金",notes = "缴纳保证金")
    @PostMapping(value = "/payEarnest")
    public Result payEarnest(ShopEarnestMoneyDeductionBO bo) {
        shopEarnestMoneyService.updatePayEarnest(bo);
        return new Result<>();
    }

    @ApiOperation(value = "退还保证金",notes = "退还保证金")
    @PostMapping(value = "/returnMoney")
    public Result payEarnest(Long shopId) {
        if (shopId == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return shopEarnestMoneyService.updateReturnMoney(shopId);
    }

    @ApiOperation(value = "解冻保证金",notes = "解冻保证金")
    @PostMapping(value = "/saveUnfreezeMarginMoney")
    public Result saveUnfreezeMarginMoney(Long shopId,BigDecimal money,String notes) {
        return payService.saveUnfreezeMarginMoney(shopId,money,notes);
    }

    @ApiOperation(value = "扣除保证金",notes = "扣除保证金")
    @PostMapping(value = "/deductionEarnest")
    public Result deductionEarnest(ShopEarnestMoneyDeductionBO bo) {
        if(bo == null || bo.getShopId () == null || bo.getId ()== null || bo.getMoney () == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        ShopEarnestMoneyDTO dto = shopEarnestMoneyService.selectById(bo.getId());
        if (bo.getMoney().compareTo(dto.getBalance()) >= 0){
            return new Result<>(CodeEnum.FAIL_100044);
        }
        PayOrderSaveBO payOrderSaveBO = new PayOrderSaveBO ();
        payOrderSaveBO.setPayType (0); //余额支付
        payOrderSaveBO.setMoney (bo.getMoney ()); //金额
        payOrderSaveBO.setType (9); //保证金
        payOrderSaveBO.setIds (bo.getShopId ().toString ()); //店铺id
        payOrderSaveBO.setNotes (bo.getNotes ()); //说明
        Result<PayOrderVO> payOrderVOResult = payService.savePay (payOrderSaveBO, false);
        if(payOrderVOResult.getData () != null){
            return new Result <>(CodeEnum.SUCCESS);
        }else {
            return new Result <>(CodeEnum.FAIL);
        }
    }

    @ApiOperation(value = "查看保证金使用记录和退还保证金记录",notes = "查看保证金使用记录和退还保证金记录")
    @PostMapping(value = "/queryPagePayOrderLog")
    public Result<Page<PayOrderLogVO>> queryPagePayOrderLog(PayOrderLogBO bo) {

        if (bo == null || bo.getShopId () == null || bo.getType () == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }
       return new Result<> (payService.queryPagePayOrderLog(bo));

    }




}
